import { Component, ViewChild, Inject } from '@angular/core';
import {IonicPage , NavController, Content, Refresher } from 'ionic-angular';

// service
import { AlertService } from '../../../providers/utils/alert.service';
import { HideBarsService } from '../../../providers/utils/hideBars.service';

import * as PageConstant from '../../../constant/pageConstant';
import { Subscription } from 'rxjs/Subscription';
/**
 * 认证中心
 */
@IonicPage()
@Component({
  selector: 'page-authentication-center',
  templateUrl: 'authentication-center.html'
})
export class AuthenticationCenterPage{

  // 用户信息
  private userInfo: any ={
    idState: 0,
    contactState: 0,
    bankState: 0,
    mobileState: 0,
    zmxyState: 0,
    workState: 0,
    moreState: 0,
  };

  // 若下来未完成不能点击下一步
  private isCanNextBool: boolean;

  @ViewChild(Content) content: Content;
  @ViewChild(Refresher) refresher: Refresher;

  // 监听 用户信息
  private userSubscription: Subscription;
  constructor(
    private alertCtrl : AlertService,
    public navCtrl: NavController,
    private hideBars: HideBarsService,
    @Inject('user') private userService,
  )
  {}

  /*** 初始化数据
   * */
  initData()
  {
    this.userSubscription= this.userService.getUser()
      .filter(user=> user.userId)
      .subscribe(user => {
        this.userInfo= {...user};
        this.isCanNextBool= true;
      })
  }

  /*** 页面进入
   * */
  ionViewWillEnter()
  {
    this.hideBars.changeTabBarsElementBottom('-5rem');
    this.initData();
  }

  ionViewDidEnter() {
    this.refresher._top = this.content.contentTop + 'px';
    this.refresher.state = 'ready';
    this.refresher._onEnd();
    // this.refresher.state = 'refreshing';
    // this.refresher._beginRefresh();
  }

  /*** 页面离开
   * */
  ionViewWillLeave()
  {
    this.hideBars.changeTabBarsElementBottom('0');
    if(typeof this.userSubscription !== 'undefined')
      this.userSubscription.unsubscribe();
  }


  /*** 打开个人信息
   * */
  public doMineHome(): void
  {
    this.navCtrl.push(PageConstant.MineHomePage,{
      idState: this.userInfo.idState,
      idInfo: {...this.userInfo.idInfo},
    });
  }

  /*** 紧急联系人
   * */
  public doMineContacts(): void
  {
    this.isCanGoNextPage(PageConstant.MineContactsPage);
  }

  /*** 打开绑定银行卡
   * */
  public doBindBankCard(): void
  {
    this.isCanGoNextPage(PageConstant.BindBankCardPage);
  }


  /*** 打开认证手机运营商
   * */
  public doMineTelephone(): void
  {
    const mobileState= this.userInfo.mobileState;
    this.isCanGoNextPage(PageConstant.MineTelephonePage,{ mobileState });
  }

  /*** 打开认证手机运营商
   * */
  public doMineAliPay(): void
  {
    this.isCanGoNextPage(PageConstant.MineAliPayPage);
  }

  /*** 打开工作信息
   * */
  public doMineWork(): void
  {
    this.isCanGoNextPage(PageConstant.MineWorkPage);
  }

  /*** 打开更多信息(社交账号)
   * */
  public doMineMore(): void
  {
    this.isCanGoNextPage(PageConstant.MineMorePage);
  }

  /*** 验证是否等打开下一页
   * @param page 要跳转的页面
   * @param data 需要传递的参数
  * */
  private isCanGoNextPage(page: string, data: any= {})
  {
    // 若下拉未完成, 不能下一步
    if(!this.isCanNextBool) return;
    //若身份认证未通过不能认证其他

    const { idState }= this.userInfo;
    if(!idState){
      if( page!== PageConstant.MineHomePage){
        this.alertCtrl.createAgreeAlert('必须先认证个人信息!');
        return;
      }
    }
    this.navCtrl.push(page,data);
  }

  /*** 上拉刷新,获取用户数据
   * */
  executeRefresh(refresher: Refresher) {
    this.isCanNextBool= false; // 初始化开关
    this.userService.getUserInfoFromBack(refresher);
  }
}

